package com.mgwt.mobile.client.xui;

import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.ui.FlowPanel;
import com.googlecode.mgwt.dom.client.event.tap.HasTapHandlers;
import com.googlecode.mgwt.dom.client.event.tap.TapHandler;
import com.googlecode.mgwt.ui.client.MGWTStyle;
import com.googlecode.mgwt.ui.client.dialog.Dialog;
import com.googlecode.mgwt.ui.client.dialog.DialogPanel;
import com.googlecode.mgwt.ui.client.dialog.HasTitleText;
import com.googlecode.mgwt.ui.client.dialog.PopinDialog;
import com.googlecode.mgwt.ui.client.theme.base.DialogCss;
import com.googlecode.mgwt.ui.client.widget.ProgressIndicator;

/**
 * A simple busy dialog that blocks input and shows a spinning/loading image.
 * 
 * @author Grant Slender
 */

public class BusyDialog implements HasTitleText, HasTapHandlers, Dialog {

	private PopinDialog popinDialog;
	private DialogPanel dialogPanel1;

	/**
	 * Construct an alert dialog
	 * 
	 * @param title
	 *            - the title of the dialog
	 * @param text
	 *            - the text of the dialog
	 */
	public BusyDialog(String title) {
		this(MGWTStyle.getTheme().getMGWTClientBundle().getDialogCss(), title);
	}

	/**
	 * Construct an alert dialog
	 * 
	 * @param css
	 *            - the css to use
	 * @param title
	 *            - the title of the dialog
	 * @param text
	 *            - the text of the dialog
	 */
	public BusyDialog(DialogCss css, String title) {
		css.ensureInjected();
		popinDialog = new PopinDialog(css);
		dialogPanel1 = new DialogPanel();
		dialogPanel1.showCancelButton(false);
		dialogPanel1.showOkButton(false);

		FlowPanel fp = new FlowPanel();
		fp.getElement().getStyle().setProperty("width", "50px");
		fp.getElement().getStyle().setProperty("marginLeft", "auto");
		fp.getElement().getStyle().setProperty("marginRight", "auto");
		fp.add(new ProgressIndicator());
		dialogPanel1.getContent().add(fp);
		popinDialog.add(dialogPanel1);

		setTitleText(title);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.mgwt.ui.client.dialog.HasTitleText#setTitleText(java.lang
	 * .String)
	 */
	/** {@inheritDoc} */
	@Override
	public void setTitleText(String text) {
		dialogPanel1.getDialogTitle().setText(text);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.mgwt.ui.client.dialog.HasTitleText#getTitleText()
	 */
	/** {@inheritDoc} */
	@Override
	public String getTitleText() {
		return dialogPanel1.getDialogTitle().getText();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.googlecode.mgwt.ui.client.dialog.Dialog#show()
	 */
	/**
	 * <p>
	 * show
	 * </p>
	 */
	public void show() {
		popinDialog.center();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.googlecode.mgwt.dom.client.event.tap.HasTapEvent#addTapHandler(com
	 * .googlecode.mgwt.dom. client.event.tap.TapHandler)
	 */
	/** {@inheritDoc} */
	@Override
	public HandlerRegistration addTapHandler(TapHandler handler) {
		return dialogPanel1.getOkButton().addTapHandler(handler);
	}

	@Override
	public void hide() {
		popinDialog.hide();

	}

}
